package com.wz.dhs.utils;


import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;

public class DateTimeUtil {

    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static DateTimeFormatter huaweiFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

    public static DateTimeFormatter durationFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");

    public static DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyyMM");
    public static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");

    public static DateTimeFormatter akDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    public static String parseAkTime(LocalDateTime time){
        return time.format(akDateTimeFormatter);
    }

    public static String parseHwTime(String time){
        LocalDateTime parse = LocalDateTime.parse(time, huaweiFormatter).plusHours(8);
        return parse.format(formatter);
    }

    public static LocalDateTime parseHwTimeToLocalTime(String time){
        LocalDateTime parse = LocalDateTime.parse(time, huaweiFormatter).plusHours(8);
        return parse;
    }

    public static LocalDateTime timestamToDatetime(Timestamp timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp.getTime());
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    public static LocalDateTime parseTime(String time) {
        return LocalDateTime.parse(time,formatter);
    }

    public static String formatTime(LocalDateTime time){
        return time.format(formatter);
    }

    public static String formatMonth(LocalDate date){
        return date.format(monthFormatter);
    }

    public static String formatDate(LocalDate date){
        return date.format(dateFormatter);
    }

    public static String formatRFCTime(LocalDateTime time){
        return time.format(durationFormatter);
    }

    public static BigDecimal processMath(String startTimeStr,String endTimeStr){
        // 任务开始时间
        LocalDateTime startTime = LocalDateTime.parse(startTimeStr,durationFormatter).plusHours(8);

        // 任务结束时间
        LocalDateTime endTime = LocalDateTime.parse(endTimeStr,durationFormatter).plusHours(8);

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 计算任务进度
        Duration duration = Duration.between(startTime, now);
        Duration totalDuration = Duration.between(startTime, endTime);

        if(duration.compareTo(totalDuration) > 0){
            return new BigDecimal("1");
        }

        BigDecimal progress = new BigDecimal(duration.getSeconds()).divide(BigDecimal.valueOf(totalDuration.getSeconds()),2,BigDecimal.ROUND_DOWN);

        // 输出任务进度（百分比）
        System.out.println("任务进度：" + progress + "%");
        return progress;
    }

    public static void main(String[] args) {
        processMath(null,null);
    }
}
